package tt.doubleagent.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;

import tt.doubleagent.tools.Utilities;

import android.content.Context;
import android.content.res.AssetManager;

public class WordDataManager {

	private String WordPackageDir = "WordPackages";
	private Context mContext;
	public String UserDefinedWordPackageID = "999";
	// all the available packages from which users can choose package from
	private HashMap<String, WordPackage> AvailablePackages = null;
	// basic packages which are not charged, enabled at any time from the app
	// private HashMap<String, WordPackage> BasicPackages = null;
	// charged packages that only be available after purchased.
	private HashMap<String, WordPackage> ChargedPackages = null;

	// user defined package id is set to 999;
	// user defined word-package. currently support only on user word-package.
	// private WordPackage UserDefinedPackage = null;

	public WordDataManager(Context context) {
		mContext = context;
		AvailablePackages = new HashMap<String, WordPackage>();
		// BasicPackages = new HashMap<String, WordPackage>();
		ChargedPackages = new HashMap<String, WordPackage>();
		// UserDefinedPackage = new WordPackage();
		LoadPackages();
	}

	// init all package array status when starting up the app
	public void LoadPackages() {
		BufferedReader reader;
		File file = mContext.getDir(WordPackageDir, mContext.MODE_PRIVATE);
		if (file.exists()) {
			File[] files = file.listFiles();
			try {
				for (File f : files) {
					FileInputStream fis = new FileInputStream(f);
					reader = new BufferedReader(new InputStreamReader(fis,
							"utf-8"));

					String packageInfoLine = reader.readLine();
					WordPackage wp = new WordPackage(packageInfoLine);
					String WordPairInfo = reader.readLine();
					while (WordPairInfo != null) {
						WordPair wpair = new WordPair(WordPairInfo);
						wp.WordPairList.add(wpair);
						WordPairInfo = reader.readLine();
					}
					fis.close();
					reader.close();

					if (wp.Purchased || wp.Price == 0) {
						AvailablePackages.put(wp.ID, wp);
					}
					if (wp.Price > 0) {
						ChargedPackages.put(wp.ID, wp);
					}

				}

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();

			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {
			// load package in resource files
			AssetManager asm = mContext.getAssets();
			try {
				String[] list = asm.list("WordPackages");
				for (String str : list) {
					InputStream inputStream = asm.open("WordPackages/" + str);
					BufferedReader br = new BufferedReader(
							new InputStreamReader(inputStream));

					File savedFile = new File(WordPackageDir);
					savedFile.mkdir();
					savedFile = new File(WordPackageDir + "/" + str);
					FileWriter fw = new FileWriter(savedFile);
					String strLine = br.readLine();
					while (strLine != null) {
						fw.write(strLine + "\r\n");
						strLine = br.readLine();
					}
					fw.close();
					br.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// load ag;
			LoadPackages();

		}
	}

	// purchase a charged packages
	public boolean PurchasePackage(int PackageID) {

		WordPackage wp = ChargedPackages.get(PackageID);
		wp.IsModified = true;
		return wp.Purchased = true;
	}

	// save user defined word pair to user word-package
	public boolean SaveUserDefinedWordPair(String Word1, String Word2) {
		// user defined word package id is 999
		WordPackage wp = AvailablePackages.get(UserDefinedWordPackageID);
		WordPair wpair = new WordPair();
		wpair.ID = Utilities.generateIG();
		wpair.Word1 = Word1;
		wpair.Word2 = Word2;
		wpair.UsedCount = 0;
		wpair.Affinity = true;
		wp.IsModified = true;
		wp.WordPairList.add(wpair);
		return true;
	}

	// set a word-pair as used
	public boolean SetWordPairUsed(int PackageID, String WordPairID) {
		WordPackage wp = AvailablePackages.get(PackageID);
		WordPair wpair = wp.getWordPair(WordPairID);
		wpair.UsedCount++;
		wp.IsModified = true;
		return true;
	}

	// get the next unused word-pair a package
	public WordPair GetNextUnusedWordPair(int PackageID) {
		WordPackage wp = AvailablePackages.get(PackageID);
		return wp.getNextUnusedWordPair();
	}

	// get a random word-pair from a package
	public WordPair GetRandomWordPair(int PackageID) {
		WordPackage wp = AvailablePackages.get(PackageID);
		return wp.getRandomWordPair();
	}

}
